home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / System 7.0 Samples / AEObject-Edition1.0.2 Sample / Initialize.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-18  |  14.1 KB  |  361 lines  |  [TEXT/MPS ]

  1. /*------------------------------------------------------------------------------
  2. *
  3. *  Apple Developer Technical Support
  4. *
  5. *  
  6. *
  7. *  Program:    AEObject-Edition Sample
  8. *  File:   Initialize.c -C Source
  9. *
  10. *  by: C.K. Haun <TR>
  11. *
  12. *  Copyright © 1990-1992 Apple Computer, Inc.
  13. *  All rights reserved.
  14. *
  15. *------------------------------------------------------------------------------
  16. * This file contains some of our window handling routines.  I created it 
  17. * primarily to take some of the clutter out of the main.c file.
  18. * It also contains the clipboard handlers
  19. *----------------------------------------------------------------------------*/
  20.  
  21. #define __INIT__
  22.  
  23. #include "Sampdefines.h"
  24. #include <SegLoad.h>
  25.  
  26. #pragma segment MyInit
  27. /* StartStuff initializes the managers and does some other init stuff */
  28. void StartStuff(void)
  29. {
  30.     long aLong;
  31.     OSErr myErr;    
  32.     MaxApplZone();
  33.     MaxMem(&aLong);
  34.     /* Nobody every says this, it's always assumed, but I'll tell yah... */
  35.     /* 'qd' is in the Runtime.o library (which is one reason you have to link it)  */
  36.     /* it doesn't come from the toolbox or any other magic place */
  37.     InitGraf((Ptr)&qd.thePort);
  38.     InitFonts();
  39.     InitWindows();
  40.     InitMenus();
  41.     TEInit();
  42.     InitDialogs(nil);
  43.     MoreMasters();
  44.     /* See if AppleEvents and the Edition manager are available */
  45.     InitAEStuff();                                          /* In AppleEventM.c */
  46.     gHasEditionManager = (Gestalt(gestaltEditionMgrAttr, &aLong) == noErr);
  47.     /* the value returned will be 1, but we don't really care here, since there aren't */
  48.     /* multiple versions of the Edition Manager yet */
  49.     if (gHasEditionManager) {
  50.         InitEditionPack();
  51.         
  52.     } else {
  53.         ModalFilterUPP upp = NewModalFilterProc(standardAlertFilter);
  54.         Alert(kNoEditionManager, upp);                      /* tell 'em and exit */
  55.         DisposeRoutineDescriptor(upp);
  56.         ExitToShell();
  57.     }
  58.     GetCurrentProcess(&gOurSN);                             /* grab our applications process serial number */
  59.     /* so we can communicate (send appleevents) to */
  60.     /* ourself easily */
  61.     myErr = Gestalt(gestaltQuickdrawVersion, &aLong);
  62.     if(myErr == noErr && (aLong & 0xff00)) 
  63.         gHasColor = true;
  64.     StartMenus();                                           /* load the menu bar and set up the menus */
  65.  
  66.     CreateClipBoard();
  67.  
  68.     CreateAEStatus();
  69.     /* ••• NOTE: I am not opening a document window here.  The application will wait for an */
  70.     /* 'oapp' or 'odoc' AppleEvent before opening a window.  This makes it a whole lot easier */
  71.     /* to deal with ODOC events, since you needn't special case anything */
  72.     gMySleep = 5;                                           /* initialize sleep at some reasonable level */
  73.     gWindObjSpecHandle = (WindowObjectDefHandle)NewHandleClear(sizeof(WindowObjectDef));
  74.     gTextObjSpecHandle = (TextObjectDefHandle)NewHandleClear(sizeof(TextObjectDef));
  75.     gShapeObjSpecHandle = (ShapeObjectDefHandle)NewHandleClear(sizeof(ShapeObjectDef));
  76.     gLocalInteraction = 0;    /* default this */
  77.     gAESendInteraction = 0;
  78.     gReplyMode = 1;
  79.     LoadPrefs();
  80.     gPreferences.prefsChanged = false;
  81.     /* and set it, it may have changed with the Prefs */
  82.      AESetInteractionAllowed(gLocalInteraction);
  83.     InitCursor();
  84.  
  85. }
  86.  
  87. /* end StartStuff */
  88.  
  89.  
  90. #pragma segment MyInit
  91.  
  92.  
  93. /* CreateClipBoard creates our invisible clipboard window at application */
  94. /* start time, and keeps it open always.  Visibility is toggled from the edit menu, */
  95. /* but the window always exists */
  96.  
  97. void CreateClipBoard(void)
  98. {
  99.     WindowPtr tempWindow;
  100.     windowCHandle tempWC;
  101.     
  102.     /* open my clipboard window */
  103.     tempWindow = GetNewWindow(kClipWindow, 0, (WindowPtr)-1);
  104.     
  105.     /* since I'm using the same function for my clipboard as for a document window, I have */
  106.     /* to change some of the fields */
  107.     ((WindowPeek)tempWindow)->windowKind = kClipboardWindow;
  108.     ((WindowPeek)tempWindow)->refCon = NewHandleClear(sizeof(windowControl));        /* add our control structure to it */
  109.     tempWC = (windowCHandle)GetWRefCon(tempWindow);         /* and put it where we can use it */
  110.     HLock((Handle)tempWC);                                  /* lock it down */
  111.  
  112.     /* add pointers to our procedures for drawing, saving, and closing */
  113.     (*tempWC)->drawMe = (ProcPtr)DrawClip;
  114.     (*tempWC)->saveMe = (ProcPtr)nil;
  115.     (*tempWC)->closeMe = (ProcPtr)CloseClip;
  116.     (*tempWC)->clickMe = (ProcPtr)ClipClick;
  117.     (*tempWC)->sizeMe = (ProcPtr)SizeClip;
  118.     (*tempWC)->currentAction = nil;
  119.     (*tempWC)->undoAction = nil;
  120.     (*tempWC)->hasSelection = false;
  121.     (*tempWC)->windowDirty = false;
  122.     (*tempWC)->boxHandle = nil;
  123.     (*tempWC)->textSections = nil;
  124.     HUnlock((Handle)tempWC);
  125.     gScrapData = NewHandle(0);
  126.     UpdateScrap(false);                                     /* not visible, don't refresh */
  127. }
  128.  
  129. /* end CreateClipBoard */
  130.  
  131.  
  132. /* CreateAEStatus creates our invisible AEStatus window at application */
  133. /* start time, and keeps it open always.  Visibility is toggled from the edit menu, */
  134. /* but the window always exists */
  135. void CreateAEStatus(void)
  136. {
  137.     WindowPtr tempWindow;
  138.     windowCHandle tempWC;
  139.     WindowPtr tempPort;
  140.     Rect dest;
  141.     /* open my clipboard window */
  142.     tempWindow = GetNewWindow(kAEStatusWindowID, 0, (WindowPtr)-1);
  143.     ((WindowPeek)tempWindow)->windowKind = kAEStatusWindow;
  144.     /* since I'm using the same function for my clipboard as for a document window, I have */
  145.     /* to change some of the fields */
  146.     ((WindowPeek)tempWindow)->refCon = NewHandleClear(sizeof(windowControl));        /* add our control structure to it */
  147.     tempWC = (windowCHandle)GetWRefCon(tempWindow);         /* and put it where we can use it */
  148.     HLock((Handle)tempWC);                                  /* lock it down */
  149.     /* add pointers to our procedures for drawing, saving, and closing */
  150.     (*tempWC)->drawMe = (ProcPtr)DrawAES;
  151.     (*tempWC)->saveMe = (ProcPtr)SaveAES;
  152.     (*tempWC)->closeMe = (ProcPtr)CloseAES;
  153.     (*tempWC)->clickMe = (ProcPtr)ClickAES;
  154.     (*tempWC)->sizeMe = (ProcPtr)SizeAES;
  155.     (*tempWC)->currentAction = nil;
  156.     (*tempWC)->undoAction = nil;
  157.     (*tempWC)->hasSelection = false;
  158.     (*tempWC)->windowDirty = false;
  159.     
  160.     (*tempWC)->textSections = nil;
  161.     (*tempWC)->fileAliasHandle = (AliasHandle)NewHandle(0);
  162.     (*tempWC)->windowDirty = false;
  163.     (*tempWC)->windowIndex = 999999; /* make abnormally large */
  164.     HUnlock((Handle)tempWC);
  165.     /* and this window has a  text edit field */
  166.     GetPort(&tempPort);                                     /* save and set port so TextEdit puts the record in the right place */
  167.     SetPort(tempWindow);
  168.     
  169.     dest = tempWindow->portRect;
  170.     dest.right -= 16;
  171.     (*tempWC)->boxHandle = TENew(&dest, &dest);
  172.     TEAutoView(true, (*tempWC)->boxHandle);                 /* make the text auto-scrolling */
  173.     SetPort(tempPort);
  174. }
  175.  
  176. /* StartMenus loads our menu bar resource, and initializes the menus 
  177. *   and some settings related to the menu items 
  178. */
  179.  
  180. void StartMenus(void)
  181. {
  182.     MenuHandle tempHandle;
  183.     StringHandle helpString;
  184.     Handle myMBar;
  185.     short count;
  186.     myMBar = GetNewMBar(kOurMenuBar);
  187.     SetMenuBar(myMBar);
  188.     gAppleMenuHandle = GetMenuHandle(kAppleMenu);
  189.     gFileMenuHandle = GetMenuHandle(kFileMenu);
  190.     gEditMenuHandle = GetMenuHandle(kEditMenu);
  191.     gToolMenuHandle = GetMenuHandle(kToolsMenu);
  192.     gAppleEventMenuHandle = GetMenuHandle(kAEMenu);
  193.     gEditionMenuHandle = GetMenuHandle(kEditionMenu);
  194.     gWindowMenuHandle = GetMenuHandle(kWindowMenu);
  195.     if(gHasColor){
  196.         gColorMenuHandle = GetMenu(kColorMenu);
  197.         InsertMenu(gColorMenuHandle,0);
  198.         CheckItem(gColorMenuHandle,kBlackColorItem,true);
  199.         }
  200.         DrawMenuBar();    
  201.     /* The following lines add my Help item to the Sys 7 Help menu. */
  202.     /* That's where help should go now, _not_ under the Apple Menu as we have  */
  203.     /* been doing for so long.  Since we now have an actual Help menu, use it, */
  204.     /* since your users will expect it. */
  205.     /* get the Help menu handle */
  206.     HMGetHelpMenuHandle(&tempHandle);
  207.     /* How many items does it have? */
  208.     count = CountMItems(tempHandle);
  209.     /* get our help string from our resource fork */
  210.     helpString = GetString(kHelpString);
  211.     DetachResource(helpString);
  212.     HNoPurge(helpString);
  213.     MoveHHi((Handle)helpString);
  214.     HLock((Handle)helpString);
  215.     /* Add our help item to the help menu */
  216.     InsertMenuItem(tempHandle, (Ptr)*helpString, count + 1);
  217.     HUnlock(helpString);
  218.     HPurge(helpString);
  219.     /* Remember what our help item number is */
  220.     gHelpItem = CountMItems(tempHandle);
  221.     
  222.     /* add heirarchical menus for AppleEvent menu */
  223.     tempHandle = GetMenu(kGetDataSubmenu);
  224.     InsertMenu(tempHandle, -1);
  225.     /* and the setdata hier */
  226.     tempHandle = GetMenu(kSetDataSubmenu);
  227.     InsertMenu(tempHandle, -1);
  228.     
  229.     AppendResMenu(gAppleMenuHandle, 'DRVR');                   /* add DAs */
  230. }
  231.  
  232. /* end StartMenus */
  233.  
  234. /* InitAEStuff checks to see if this machine has AppleEvents and 
  235. *   does our setup.
  236. *   If AppleEvents are not found, we alert and exit.
  237. *   This is also the place where all the handlers for AppleEvents we deal
  238. *   with are installed.  This includes the required AppleEvents, and the
  239. *   Edition Manager specific ones used in this app.
  240. */
  241. #pragma segment MyInit
  242. void InitAEStuff(void)
  243. {
  244.     extern Boolean gHasAppleEvents;
  245.     long aLong;
  246.  
  247.     // leaks Routine Descriptors, but we'll live
  248.     AEinstalls HandlersToInstall[] =  {
  249.         /* The above are the four required AppleEvents. */ {
  250.             kCoreEventClass, kAEOpenApplication, NewAEEventHandlerProc(AEOpenHandler)
  251.         },  {
  252.             kCoreEventClass, kAEOpenDocuments, NewAEEventHandlerProc(AEOpenDocHandler)
  253.         },  {
  254.             kCoreEventClass, kAEPrintDocuments, NewAEEventHandlerProc(AEPrintHandler)
  255.         },  {
  256.             kCoreEventClass, kAEQuitApplication, NewAEEventHandlerProc(AEQuitHandler)
  257.         }, 
  258.         /* These are the five Edition Manager AppleEvents  */ {
  259.             kCoreEventClass, kAECreatePublisher, NewAEEventHandlerProc(AECreatePubHandler)
  260.         },  {
  261.             sectionEventMsgClass, sectionReadMsgID, NewAEEventHandlerProc(AEReadSectionHandler)
  262.         },  {
  263.             sectionEventMsgClass, sectionWriteMsgID, NewAEEventHandlerProc(AEWriteSectionHandler)
  264.         },  {
  265.             sectionEventMsgClass, sectionScrollMsgID, NewAEEventHandlerProc(AEScrollSectionHandler)
  266.         },  {
  267.             sectionEventMsgClass, sectionCancelMsgID, NewAEEventHandlerProc(AECancelSectionHandler)
  268.         }, 
  269.         /*  some core event handlers */ {
  270.             kAECoreSuite, kAEGetData, NewAEEventHandlerProc(AEGetDataHandler)
  271.         },  {
  272.             kAECoreSuite, kAESetData, NewAEEventHandlerProc(AESetDataHandler)
  273.         },  {
  274.             kAECoreSuite, kAEAnswer, NewAEEventHandlerProc(AEAnswerHandler)
  275.         }
  276.     };
  277.     
  278.     // leaks Routine Descriptors, but we'll live
  279.     static CoercionInstalls CoercionsToInstall[] =  {
  280.         {
  281.             typeAlias, typeTargetID, NewAECoercePtrProc(CoerceAliasToTargetID), false
  282.         },  {
  283.             typeBoolean, typeChar, NewAECoercePtrProc(CoerceBooleanToChar), false
  284.         },  {
  285.             typeQDRectangle, typeChar, NewAECoercePtrProc(CoerceQDRectToChar), false
  286.         }, 
  287.         /* and two little coercions I just had to have... */ {
  288.             typeChar, typeMyPString, NewAECoercePtrProc(CoerceCharToPString), false
  289.         },  {
  290.             typeMyPString, typeChar, NewAECoercePtrProc(CoercePStringToChar), false
  291.         }
  292.     };
  293.     
  294.     OSErr aevtErr = noErr;
  295.     aLong = 0;
  296.     /* Check this machine for AppleEvents.  If they are not here (ie not 7.0)
  297.     *   then we exit */
  298.     gHasAppleEvents = (Gestalt(gestaltAppleEventsAttr, &aLong) == noErr);
  299.     /* The following series of calls installs all our AppleEvent Handlers.
  300.     *   These handlers are added to the application event handler list that 
  301.     *   the AppleEvent manager maintains.  So, whenever an AppleEvent happens
  302.     *   and we call AEProcessEvent, the AppleEvent manager will check our
  303.     *   list of handlers and dispatch to it if there is one.
  304.     */
  305.     if (gHasAppleEvents) {
  306.         register qq;
  307.         for (qq = 0; qq < ((sizeof(HandlersToInstall) / sizeof(AEinstalls))); qq++) {
  308.             aevtErr = AEInstallEventHandler(HandlersToInstall[qq].theClass, HandlersToInstall[qq].theEvent,
  309.                                             HandlersToInstall[qq].theProc, 0, false);
  310.             
  311.             if (aevtErr) {
  312.                 ModalFilterUPP upp = NewModalFilterProc(standardAlertFilter);
  313.                 Str31 text1, text2;
  314.                 NumToString((long)aevtErr, text1);
  315.                 text2[0] = 4;
  316.                 BlockMove((Ptr)&HandlersToInstall[qq].theEvent, (Ptr)text2, 4);
  317.                 ParamText(text1, text2, "", "");
  318.                 NoteAlert(kBadHInstall, upp);
  319.                 DisposeRoutineDescriptor(upp);
  320.             }
  321.         }
  322.         /* now our coercion routines */
  323.         for (qq = 0; qq < ((sizeof(CoercionsToInstall) / sizeof(AEinstalls))); qq++) {
  324.             aevtErr = AEInstallCoercionHandler(CoercionsToInstall[qq].fromType, CoercionsToInstall[qq].toType,
  325.                                                CoercionsToInstall[qq].theProc, nil, false, false);
  326.             if (aevtErr) {
  327.                 ModalFilterUPP upp = NewModalFilterProc(standardAlertFilter);
  328.                 Str31 text1, text2;
  329.                 NumToString((long)aevtErr, text1);
  330.                 text2[0] = 4;
  331.                 BlockMove((Ptr)&CoercionsToInstall[qq].fromType, (Ptr)text2, 4);
  332.                 ParamText(text1, text2, "", "");
  333.                 NoteAlert(kBadHInstall, upp);
  334.                 DisposeRoutineDescriptor(upp);
  335.             }
  336.         }
  337.        
  338.    } else {
  339.         ModalFilterUPP upp = NewModalFilterProc(standardAlertFilter);
  340.         Alert(kNoAppleEvents, upp);
  341.         DisposeRoutineDescriptor(upp);
  342.         ExitToShell();
  343.     }
  344.     /* this function initializes the OSL and installs our handlers */
  345.     aevtErr = InstallObjectHandlers();
  346.     if (aevtErr) {
  347.         
  348.         ShowMe("\p Object Lib error, failing and exiting", aevtErr, __LINE__);
  349.         ExitToShell();
  350.     }
  351.     /* and one more thing, create my null descriptor to build all my objects from. */
  352.     /* you may want to do this each time, personal choice */
  353.     aevtErr = AECreateDesc(typeNull, nil, 0, &gNullDesc);
  354.     mAEErrorDisplay("\pCreating nul desc", aevtErr)
  355. }
  356.  
  357. /* end InitAEStuff */
  358.  
  359.  
  360. #undef __INIT__
  361.